package vn.info.vietnaminfo.adapter;

import java.util.ArrayList;
import java.util.List;

import vn.info.vietnaminfo.R;
import vn.info.vietnaminfo.data.DSDiaDiem;
import vn.info.vietnaminfo.lazylist.ImageLoader;
import vn.info.vietnaminfo.utli.GlobalVariable;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class RetailerAdapter extends BaseAdapter{
	private LayoutInflater mInflater;
	private List<DSDiaDiem> listRetailer = null;
	private Context mContext;
	private ImageLoader imageLoader; 
	private final String TAG = "RetailerAdapter";
	private ArrayList<DSDiaDiem> listRetailerOrigin;

	public RetailerAdapter(Context _context, List<DSDiaDiem> _listRetailer)
	{
		mContext  		= _context;
		listRetailer   	= _listRetailer;
		imageLoader 	= new ImageLoader(mContext);
		mInflater 		= LayoutInflater.from(mContext);

		this.listRetailerOrigin = new ArrayList<DSDiaDiem>();
		if(listRetailer!=null)
			this.listRetailerOrigin.addAll(listRetailer);
	}

	public class ViewHolder
	{
		LinearLayout	linearRowSelect;
		TextView 		tvNameShop;
		TextView 		tvTitleShop;
		TextView 		tvAddressShop;
		TextView 		tvNumberPhoneShop;
		ImageView 		imgImageShop;
	}

	@Override
	public View getView(final int position, View view, ViewGroup parent)
	{
		final ViewHolder holder;
		if (view == null)
		{
			holder 					= new ViewHolder();
			view 					= mInflater.inflate(R.layout.layout_retailer_row, null);
			holder.linearRowSelect	= (LinearLayout)view.findViewById(R.id.linearRowSelect);
			holder.tvNameShop		= (TextView)view.findViewById(R.id.tvNameShop);
			holder.tvTitleShop 		= (TextView)view.findViewById(R.id.tvTitleShop);
			holder.tvAddressShop 	= (TextView)view.findViewById(R.id.tvAddressShop);
			holder.tvNumberPhoneShop= (TextView)view.findViewById(R.id.tvNumberPhoneShop);
			
			holder.imgImageShop		= (ImageView)view.findViewById(R.id.imgShop);
			view.setTag(holder);
		}
		else
		{
			holder = (ViewHolder) view.getTag();
		}


		holder.tvNameShop.setText(listRetailer.get(position).getTenDiaDiem());
		holder.tvTitleShop.setText(listRetailer.get(position).getTieuDe());
		holder.tvAddressShop.setText("Địa chỉ: "+listRetailer.get(position).getDiaChi());
		holder.tvNumberPhoneShop.setText("Số ĐT: "+listRetailer.get(position).getPhoneNumber());


		imageLoader.DisplayImage(listRetailer.get(position).getURLImage(), holder.imgImageShop);


		holder.tvNameShop.setTag(position);
		holder.tvTitleShop.setTag(position);
		holder.tvAddressShop.setTag(position);
		holder.tvNumberPhoneShop.setTag(position);
		holder.imgImageShop.setTag(position);

		return view;		
	}


	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		try
		{
			return listRetailer.size();
		}catch(Exception e){
			return 0;
		}
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	public void filter(String charText) {
		charText = charText.toLowerCase();
		listRetailer.clear();
		if (charText.length() == 0) {
			listRetailer.addAll(listRetailerOrigin);
		} else {
			for (DSDiaDiem retailer : listRetailerOrigin) {
				if (retailer.getTenDiaDiem().toLowerCase().contains(charText)||

						GlobalVariable.RemoveSign4VietnameseString(retailer.getTenDiaDiem().toLowerCase()).contains(GlobalVariable.RemoveSign4VietnameseString(charText))

						) {
					listRetailer.add(retailer);
				}
			}
		}
		notifyDataSetChanged();
	}



}
